<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        .custom-tree-node {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-size: 14px;
            padding-right: 8px;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="filter-container">
        <el-button type="primary" icon="el-icon-plus" class="butT" @click="handleAddFirstMenu()">添加一级菜单</el-button>
    </div>
    <div class="custom-tree-container" id="tree">
        <div class="block">
            <el-tree
                    :data="data"
                    show-checkbox
                    node-key="id"
                    default-expand-all
                    :expand-on-click-node="false"
                    :props="defaultProps">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
                  type="text"
                  size="small"
                  icon="el-icon-plus"
                  @click="() => append(data)">
            添加子菜单
          </el-button>
          <el-button
                  type="text"
                  size="small "
                  icon="el-icon-delete"
                  @click="() => remove(node, data)">
            删除菜单
          </el-button>
        </span>
      </span>
            </el-tree>
        </div>
    </div>
    <!-- 新增标签弹层 -->
    <div class="add-form">
        <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                     label-width="100px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="菜单名称" prop="name">
                            <el-input v-model="formData.name"/>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="linkUrl" prop="linkUrl">
                            <el-input v-model="formData.linkUrl"/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelAdd()">取消</el-button>
                <el-button type="primary" @click="handleAdd()">确定</el-button>
            </div>
        </el-dialog>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>

<script>
    var tree=new Vue({
            el:"#app",
            data() {
                const data = [];
                return {
                    data: JSON.parse(JSON.stringify(data)),
                    data: JSON.parse(JSON.stringify(data)),
                    defaultProps: {
                        children: 'children',
                        label: 'name'
                    },
                    formData:{},
                    dialogFormVisible: false,
                    rules: {//校验规则
                        name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}]
                    }
                }
            },

            methods: {
                append(data) {
                    //显示新增标签弹层
                    this.dialogFormVisible = true;
                    //获取菜单父id
                    this.formData.parentMenuId = data.id;
                    //获取子菜单path
                    var parentPath=data.path;
                    var length = data.children.length;
                    var subPath;
                    if(data.parentMenuId)
                         subPath=parentPath+"-"+(length+1);
                    else
                         subPath="/"+parentPath+"-"+(length+1)
                    this.formData.path = subPath;
                    //获取子菜单priority
                    var subPriority=length+1;
                    this.formData.priority = subPriority;
                    //获取子菜单level
                    var level = subPath.split("-").length;
                    this.formData.level = level;
                },

                remove(node, data) {
                        this.$confirm('此操作将永久删除该菜单及其子菜单, 是否继续?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            //获取当前节点id
                            var id = node.id;
                            //请求删除
                            axios.get('/menu/deleteMenuById.do?id='+id)
                                .then((res)=>{
                                    this.$message({
                                        type: res.data.flag?"success":"error",
                                        message: res.data.message
                                    });
                                    if (res.data.flag) {
                                        const parent = node.parent;
                                        const children = parent.data.children || parent.data;
                                        const index = children.findIndex(d => d.id === data.id);
                                        children.splice(index, 1);
                                    }
                                });
                        }).catch(() => {
                            this.$message({
                                type: 'info',
                                message: '已取消删除'
                            });
                        });

                },
                //添加子菜单
                handleAdd(){
                    //表单数据是否合法
                    this.$refs['dataAddForm'].validate((result)=> {
                        if(result) {
                            axios.post('/menu/addSubmenu.do',this.formData)
                                .then((res)=> {
                                    this.$message({
                                        message:res.data.message,
                                        type:res.data.flag?"success":"error"
                                    });
                                    this.resetForm();
                                    this.showMenusTree();
                                });
                        }else {
                            this.$message.error("表单校验失败");
                            return false;
                        }
                    });

                },
                //数据回显
                showMenusTree() {
                    //菜单数据回显
                    axios.post('/menu/queryAllMenus.do')
                        .then((res)=>{
                            if(res.data.flag) {
                                this.data = res.data.data;
                            }else {
                                this.$message.error("系统繁忙请稍后再试");
                            }
                        });
                },
                //重置表单
                resetForm() {
                    //隐藏表单
                    this.dialogFormVisible = false;
                    //清空表单
                    this.formData = {};
                },
                //取消添加菜单
                cancelAdd() {
                    this.resetForm();
                },
                //添加一级菜单
                handleAddFirstMenu() {
                    //显示新增标签弹层
                    this.dialogFormVisible = true;
                    //获取path
                    var path = this.data.length;
                    this.formData.path = path;
                    //获取priority
                    this.formData.priority = 1;
                    //获取level
                    this.formData.level=1;
                }

            },
            created:function () {
                //数据回显
                this.showMenusTree();
            }
        }
    )
</script>
</html>